package com.stormrage.edu.serv.learn.impl;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.learn.LeaStudentCourseDao;
import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.learn.LeaStudentCourseServ;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.edu.serv.learn.LeaStudentVideoServ;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.rep.LeaStudentCourseRep;
import com.stormrage.entity.learn.rep.LeaStudentVideoRep;
import com.stormrage.entity.learn.req.LeaStudentCourseReq;
import com.stormrage.entity.learn.req.LeaStudentVideoReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.BeanUtil;
import com.stormragetech.core.util.NumberUtil;

@Service
public class LeaStudentCourseServImpl implements LeaStudentCourseServ {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudentCourseServImpl.class);

	@Autowired
	private LeaStudentCourseDao leaStudentCourseDao;

	@Autowired
	private EduMajorServ eduMajorServ;

	@Autowired
	private LeaStudentServ leaStudentServ;

	@Autowired
	private LeaStudentVideoServ leaStudentVideoServ;

	@Override
	public ListRep queryRepListByStuId(final LeaStudentCourseReq arg0) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 LeaStudentCourseServ 】- 【 queryRepListByStuId 】- 【 STARTED 】");

		// ------------------ 获取学员信息 ------------------
		LeaStudent stu = leaStudentServ.queryById(arg0.getStuId());

		// ------------------ 获取专业信息 ------------------
		EduMajor major = eduMajorServ.queryByBunchId(stu.getBunchId());

		Map<String, Object> map = BeanUtil.initQuery(arg0.getPage());
		map.put("stuId", arg0.getStuId());

		Integer count = null;
		List<LeaStudentCourseRep> array = null;
		int seq = arg0.getPage().getStartPage() + 1;

		// ------------------ 获取单一课组课程信息 ------------------
		if (major.getModule().equals(0)) {
			count = leaStudentCourseDao.queryRepCountByStuIdForSelect(map);
			array = leaStudentCourseDao.queryRepListByStuIdForSelect(map);
		}
		// ------------------ 获取多个课组课程信息 ------------------
		if (major.getModule().equals(1)) {
			count = leaStudentCourseDao.queryRepCountByStuId(map);
			array = leaStudentCourseDao.queryRepListByStuId(map);
		}

		Iterator<LeaStudentCourseRep> it = array.iterator();
		while (it.hasNext()) {
			LeaStudentCourseRep rep = it.next();

			// ------------------ 获取此课程所有课件信息 ------------------
			LeaStudentVideoReq req = LeaStudentVideoReq.instance(arg0.getStuId(), rep.getId(), null, null);
			List<LeaStudentVideoRep> videoArray = leaStudentVideoServ.queryListForSelectStudy(req);

			String study = "0%";
			Integer studyLen = 0;
			Integer totalLen = 0;

			// ------------------ 计算学习进度 ------------------
			if (null != videoArray && videoArray.size() > 0) {

				Iterator<LeaStudentVideoRep> iter = videoArray.iterator();
				while (iter.hasNext()) {
					LeaStudentVideoRep videoRep = iter.next();
					if (null == videoRep.getLen()) {
						videoRep.setLen(0);
					}
					studyLen += videoRep.getLen();
					totalLen += videoRep.getTlen();
				}

				study = NumberUtil.initPerc(
						new BigDecimal(studyLen).divide(new BigDecimal(totalLen), 2, BigDecimal.ROUND_HALF_UP));
			}

			rep = LeaStudentCourseRep.instance(rep, seq, study, videoArray.size());
			seq++;
		}

		ListRep rep = ListRep.instance(count, array);

		logger.debug(" 【 Service 】- 【 LeaStudentCourseServ 】- 【 queryRepListByStuId 】- 【 COMPLETED 】");

		return rep;

	}

}
